<template>
  <div class="page-head">
    <div class="menu-box">
      <div class="menu-box-left">
        <RouterLink to="/home" class="alink">
          <div class="icon iconhome"></div>
          返回首页
        </RouterLink>
        <RouterLink to="/alarm" class="alink">
          <div class="icon iconalarm"></div>
          告警管理</RouterLink
        >
        <RouterLink to="/monitor" class="alink"
          ><div class="icon iconmonitor"></div>
          视频监测</RouterLink
        >
      </div>
      <div class="menu-box-middle"></div>
      <div class="menu-box-right">
        <RouterLink to="/equipment" class="alink"
          ><img class="icon iconequipment" />设备管理</RouterLink
        >
        <div></div>
        <div class="next-action" @click="nextAcation">
          <img class="next-icon" src="@/assets/img/logo.jpg" alt="" srcset="" />
        </div>
      </div>
    </div>
    <div class="page-head-left">
      <div class="weather">
        <div class="weather-icon">
          <img :src="weaterData.iconUrl || ''" alt="" class="icon" />
        </div>
        <div class="weather-content">
          <div class="tody-time">
            {{ currentDate.date }} {{ currentDate.hour }}:{{ currentDate.min }}
            <div class="week">星期{{ currentDate.week }}</div>
          </div>
          <div class="temperature">
            <div class="temperature-left">
              <div class="temperature-txt">
                {{ weaterData.tempLow }}
                <div class="temperature-unit">℃</div>
              </div>
              <div class="l">-</div>
              <div class="temperature-txt">
                {{ weaterData.tempHigh }}
                <div class="temperature-unit">℃</div>
              </div>
            </div>
            <div class="temperature-right">天气：{{ weaterData.type }}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="page-head-middle"></div>
    <div class="page-head-right">
      <AccountDropdown :label="`${nickName}~您好!`" @itemClick="acdClick" />
      <div class="message" @click="openMessage">
        <div v-if="hasmessage" class="dot">{{ messageToatl }}</div>
      </div>
    </div>
    <el-drawer v-model="drawer" :direction="'rtl'">
      <template #header>
        <div class="drawer-w-title">消息</div>
      </template>
      <template #default>
        <div class="message-list">
          <div
            class="message-item"
            v-for="(item, index) in messageList"
            :key="index"
            @click="messageItemClick(item)"
          >
            <div class="item-left">
              <img :src="message_icon" class="icon" />
              {{ item.taskName }}
            </div>
            <div class="item-right">
              {{ item.createTimeStr }}
            </div>
          </div>
        </div>
      </template>
    </el-drawer>
    <el-dialog
      v-model="dialogVisible"
      title="提示"
      width="500"
      :before-close="handleClose"
    >
      <div>{{ dialogData.taskName }}</div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="farmCropTaskBtn(0)" :loading="btnloading0"
            >结束</el-button
          >
          <el-button
            type="primary"
            @click="farmCropTaskBtn(1)"
            :loading="btnloading0"
          >
            通过
          </el-button>
          <el-button
            type="danger"
            @click="farmCropTaskBtn(2)"
            :loading="btnloading0"
            >驳回</el-button
          >
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted, toRaw } from 'vue'
import AccountDropdown from '@/components/account-dropdown/index.vue'
import { useRoute, useRouter } from 'vue-router'
import dayjs from 'dayjs'
import {
  getWeatherInfo,
  getInfo,
  logout,
  farmCropTaskList,
  farmCropTaskapproved,
  farmCropTaskreject,
  farmCropTaskend
} from '@/api'
import { clearSession } from '@/utils/auth'

import imgUrl41 from '@/assets/img/41.png'
import imgUrl42 from '@/assets/img/42.png'
import imgUrl43 from '@/assets/img/43.png'

import weather1 from '@/assets/img/weather1.png' // 晴
import weather2 from '@/assets/img/weather2.png' // 小雨
import weather3 from '@/assets/img/weather3.png' // 打雷
import weather4 from '@/assets/img/weather4.png' // 雪
import weather5 from '@/assets/img/weather5.png' // 大雨
import message_icon from '@/assets/img/message.png' // 大雨
import { fa } from 'element-plus/es/locales.mjs'
const currentRoute = useRoute()
const router = useRouter()
const weekZh = ['日', '一', '二', '三', '四', '五', '六']
let tackTimer = null
const weaterIcon = {
  1: weather1,
  2: weather2,
  3: weather3,
  4: weather4,
  5: weather5
}
const currentDate = ref({
  date: '',
  week: '',
  hour: '',
  min: ''
})

const weaterData = ref({
  tempHigh: '',
  tempLow: '',
  type: '',
  iconUrl: ''
})

const hasmessage = ref(true)

const nickName = ref('')

const drawer = ref(false)

const messageToatl = ref(0)

const messageList = ref([])

const dialogVisible = ref(false)

const dialogData = ref({})

const btnloading0 = ref(false)

const getTime = () => {
  // 获取时间
  const now = dayjs()
  const week = now.day()
  currentDate.value = {
    date: now.format('YYYY-MM-DD'),
    week: weekZh[week],
    hour: now.hour(),
    min: now.minute()
  }
}

const acdClick = (param) => {
  if (param === 1) {
    // 退出
    logout().then((res) => {
      if (res.code == 200) {
        clearSession()
        const _loction = window.location
        const _origin = _loction.origin
        const href = _origin.substring(0, _origin.lastIndexOf(':'))
        window.location.href = href + '/login'
      }
    })
  }
}

onMounted(() => {
  getTime()
  setInterval(() => {
    // 每隔10秒刷新一次时间
    getTime()
  }, 10000)
  getWeatherInfo({
    cityId: '101191304'
  }).then((res) => {
    if (res.code === 200) {
      const data = res.data[0]
      weaterData.value = {
        tempHigh: data.tempHigh,
        tempLow: data.tempLow,
        type: data.type,
        iconUrl: weaterIcon[data.icon]
      }
    }
  })
  getInfo().then((res) => {
    if (res.code === 200) {
      const _user = res.user
      nickName.value = _user.nickName
    }
  })
  getfarmCropTaskList()
  tackTimer = setInterval(() => {
    getfarmCropTaskList()
  }, 60000)
})

onUnmounted(() => {
  clearInterval(tackTimer)
})
const getfarmCropTaskList = () => {
  farmCropTaskList({
    taskStatus: 1,
    pageNum: 1,
    pageSize: 50
  }).then((res) => {
    console.log('消息', res)
    if (res.code === 200) {
      messageList.value = res.rows || []
      messageToatl.value = res.total || 0
    }
  })
}
const openMessage = () => {
  // 打开消息
  drawer.value = true
}

const messageItemClick = (param) => {
  const _data = toRaw(param)

  dialogVisible.value = true
  dialogData.value = _data
}

const api = [farmCropTaskend, farmCropTaskapproved, farmCropTaskreject]

const farmCropTaskBtn = (param) => {
  if (btnloading0.value) {
    return false
  }
  btnloading0.value = true
  api[param]({
    id: dialogData.value.id
  })
    .then((res) => {
      if (res.code === 200) {
        dialogVisible.value = false
        dialogData.value = {}
        getfarmCropTaskList()
      }
    })
    .finally(() => {
      btnloading0.value = true
    })
}
const handleClose = () => {
  dialogVisible.value = false
  btnloading0.value = false
  dialogData.value = {}
}
console.log('当前路由', currentRoute)
const nextAcation = () => {
  if (currentRoute.path == '/home') {
    router.push('/alarm')
  } else if (currentRoute.path == '/alarm') {
    router.push('/monitor')
  } else if (currentRoute.path == '/monitor') {
    router.push('/equipment')
  } else if (currentRoute.path == '/greenhouse') {
    router.push('/home')
  }
}
</script>

<style lang="scss" scoped>
.page-head {
  width: 100%;
  height: 86px;
  background: url('@/assets/img/Tabicon.png') no-repeat center;
  background-size: 100% 100%;
  position: relative;
  .menu-box {
    height: 43px;
    display: grid;
    grid-template-columns: 450px 1fr 450px;
    .menu-box-left {
      display: grid;
      grid-template-columns: 1fr 1fr 1fr;
    }
    .menu-box-right {
      display: grid;
      grid-template-columns: 1fr 1fr 1fr;
    }
    .menu-box-middle {
      display: flex;
      justify-content: center;
      align-items: center;
      font-family: FZZYJW;
      font-weight: normal;
      font-size: 35px;
      color: #ffffff;
      line-height: 20px;
    }
  }
  .page-head-left {
    position: absolute;
    left: 0px;
    bottom: 0px;
    display: flex;
    justify-content: flex-start;
    .weather {
      position: absolute;
      left: 38px;
      bottom: 0px;
      transform: translateY(50%);
      display: flex;
      justify-content: flex-start;
      align-items: stretch;
      gap: 15px;
      .weather-icon {
        width: 58px;
        height: 37px;
        .icon {
          width: 58px;
          height: 37px;
        }
      }
      .weather-content {
        .tody-time {
          width: 240px;
          display: flex;
          justify-content: flex-start;
          font-family: DIN;
          font-weight: bold;
          font-size: 20px;
          color: #ffffff;
          line-height: 13px;
          gap: 4px;
          .week {
            font-family: AlibabaPuHuiTi-Regular;
            font-size: 18px;
            color: #ffffff;
            font-weight: normal;
            line-height: 13px;
          }
        }
        .temperature {
          display: flex;
          justify-content: flex-start;
          align-items: center;
          gap: 12px;
          margin-top: 9px;
          .temperature-left {
            display: flex;
            justify-content: flex-start;
            gap: 4px;
            .temperature-txt {
              font-family: DIN;
              font-weight: bold;
              font-size: 20px;
              color: #ffffff;
              line-height: 13px;
              display: flex;
              justify-content: flex-start;
              align-items: baseline;
              .temperature-unit {
                font-weight: lighter;
                font-size: 14px;
                color: #ffffff;
                line-height: 13px;
              }
            }
            .l {
              font-weight: lighter;
              font-size: 14px;
              color: #ffffff;
              line-height: 13px;
            }
          }
          .temperature-right {
            font-family: Microsoft YaHei;
            font-weight: 400;
            font-size: 14px;
            color: #ffffff;
          }
        }
      }
    }
    .area {
      width: 160px;
      position: absolute;
      left: 390px;
      bottom: 0px;
    }
  }
  .page-head-middle {
    position: absolute;
    width: 700px;
    height: 50px;
    top: 7px;
    left: 50%;
    transform: translateX(-50%);

    background: url('@/assets/img/title.png') no-repeat center;
    background-size: contain;
  }
  .page-head-right {
    position: absolute;
    right: 0px;
    bottom: 0px;
    padding: 0px 30px;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    gap: 30px;
    .message {
      width: 20px;
      height: 20px;
      background: url('@/assets/img/message.png') no-repeat center;
      background-size: 100% 100%;
      position: relative;
      .dot {
        width: 12px;
        height: 12px;
        background: #ff0000;
        border-radius: 50%;
        border: 1px solid #ffffff;
        position: absolute;
        right: 0px;
        top: 0px;
        transform: translate(50%, -50%);
        font-family: Microsoft YaHei;
        font-weight: 400;
        font-size: 8px;
        color: #ffffff;
        line-height: 13px;
        display: flex;
        justify-content: center;
        align-items: center;
      }
    }
  }
  .next-action {
    // padding-top: 2px;
    // box-sizing: border-box;
    // height: 50px;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    .next-icon {
      width: 80px;
    }
  }
  .alink {
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 7px;
    text-decoration: none;
    font-family: Source Han Sans CN;
    font-weight: 400;
    font-size: 18px;
    color: #fff;
    line-height: 18px;
    position: relative;
    .iconhome {
      background: url('@/assets/img/40.png') no-repeat;
      background-size: 100% 100%;
    }
    .icon {
      width: 20px;
      height: 20px;
      display: block;
    }
    .iconhome {
      background: url('@/assets/img/index_01.png') no-repeat;
      background-size: 100% 100%;
    }
    .iconalarm {
      background: url('@/assets/img/mng_01.png') no-repeat;
      background-size: 100% 100%;
    }
    .iconmonitor {
      background: url('@/assets/img/vdo_01.png') no-repeat;
      background-size: 100% 100%;
    }
    .iconequipment {
      background: url('@/assets/img/sbgl_01.png') no-repeat;
      background-size: 100% 100%;
    }
    &.router-link-active {
      color: #00fcf3;
      .iconhome {
        background: url('@/assets/img/index_02.png') no-repeat;
        background-size: 100% 100%;
      }
      .iconalarm {
        background: url('@/assets/img/mng_02.png') no-repeat;
        background-size: 100% 100%;
      }
      .iconmonitor {
        background: url('@/assets/img/vdo_02.png') no-repeat;
        background-size: 100% 100%;
      }
      .iconequipment {
        background: url('@/assets/img/sbgl_02.png') no-repeat;
        background-size: 100% 100%;
      }
    }
    &.router-link-active::after {
      content: '';
      display: block;
      width: 92px;
      height: 3px;

      background: linear-gradient(269deg, #24df84, #3dffe6);
      position: absolute;
      left: 50%;
      bottom: -1px;
      transform: translateX(-50%);
    }
  }
  .drawer-w-title {
    font-size: 23px;
    color: #000;
    font-weight: 600;
  }
  .message-list {
    .message-item {
      height: 40px;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      padding: 0px 20px;
      box-sizing: border-box;
      gap: 8px;
      cursor: pointer;
      &:hover {
        background: #f5f5f5;
      }
      .item-left {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        gap: 8px;
        flex: 1;
        font-size: 16px;
        .icon {
          display: block;
          width: 20px;
          height: 20px;
        }
      }
       .item-right {
        font-size: 16px;
      }
    }
  }
}
</style>
